package com.share.schedule.xxl.schedule;

import com.alibaba.fastjson.JSON;
import com.share.common.utils.DateUtil;
import com.share.common.utils.DingDingUtil;
import com.share.common.utils.HttpUtil;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Slf4j
@Component
public class CallHttpJob {
    @XxlJob("callHttp")
    public void callHttp() throws Exception {
        LocalDateTime start = LocalDateTime.now();
        // 分片参数
        int shardIndex = XxlJobHelper.getShardIndex();
        int shardTotal = XxlJobHelper.getShardTotal();

        XxlJobHelper.log("", shardIndex, shardTotal);
        String msg = "分片参数：当前分片序号 = {}, 总分片数 = {}";
        log.info(msg, shardIndex, shardTotal);
        XxlJobHelper.log(msg, shardIndex, shardTotal);

        //计算请求参数
        String jobParam = XxlJobHelper.getJobParam();
        log.info("接收到参数JobParam={}", jobParam);
        Map<String, Integer> map = calaParam(jobParam);
        Set<String> urls = map.keySet();

        for (String url : urls) {
            Integer cycle = map.get(url);
            while (cycle-- > 0) {
                String realParam = null;
                try {
                    log.error("即将请求url={}", url);
                    realParam = HttpUtil.get(url);
                    log.error("请求url={},result={}", url, realParam);
                } catch (Exception e) {
                    log.error("请求异常url={}", url);
                }
            }
        }

        String title = "调用http";
        String content = title + " " + DateUtil.getDateTime() + System.lineSeparator() + JSON.toJSONString(urls);
        DingDingUtil.sendMdMsg(title, content);

        LocalDateTime end = LocalDateTime.now();
        msg = "共耗时：{}毫秒";
        long costTime = Duration.between(start, end).toMillis();
        log.info(msg, costTime);
        XxlJobHelper.log(msg, costTime);
    }

    private Map<String, Integer> calaParam(String jobParam) throws Exception {
        Map<String, Integer> listParam = new HashMap<>();

        if (StringUtils.isNotBlank(jobParam)) {
            String lineStr = jobParam.replaceAll("\\r", "").replaceAll("\\n", "");
            XxlJobHelper.log("一行参数lineStr", lineStr);
            log.info("一行参数lineStr={}", lineStr);
            String[] split = lineStr.split(";");
            for (String str : split) {
                if (StringUtils.isBlank(str)) {
                    continue;
                }
                String[] kv = str.trim().split(",");
                String url = kv[0].trim();
                if (!url.startsWith("http")) {
                    continue;
                }
                Integer attackNum = 1;
                if (kv.length == 2) {
                    try {
                        attackNum = Integer.parseInt(kv[1]);
                    } catch (Exception e) {
                        attackNum = 1;
                    }
                }
                listParam.put(url, attackNum);
            }
        }

        return listParam;
    }
}
